package com.mhc.haval.controller;

import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.constant.RoleCodeEnum;
import com.mhc.haval.security.constant.Authorities;
import com.mhc.haval.security.model.Role;
import com.mhc.haval.security.model.User;
import com.mhc.haval.vo.RoleVO;
import com.subaru.common.entity.BizResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author Churry
 * @create 2017-08-15 10:01
 **/
@Slf4j
@Controller
public class LoginController {

    /**
     * 系统首页
     *
     * @return
     */
    @RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET)
    public String index(@AuthenticationPrincipal User logedUser, ModelMap modelMap, HttpServletRequest request) {
        String sessionId = request.getSession().getId();
        log.info("Request Session : " + sessionId);
        Optional<User> userOptional = Optional.ofNullable(logedUser);
        Optional<String> hasRole = userOptional.map(user -> {
            modelMap.put("User", user);
            for (Role role : user.getRoleList()) {
                if (role.getRoleCode().equals(Authorities.ROLE_SYSADMIN)) {
                    return "index";
                }
            }
            return "accessDeny";
        });
        return hasRole.orElse("index");
    }

    /**
     * 系统登录页面
     *
     * @return
     */
    @RequestMapping(value = "/loginPage", method = RequestMethod.GET)
    public Object loginPage() {
        return "index";
    }

    /**
     * 获取登录用户详情
     *
     * @param logedUser
     * @return
     */
    @RequestMapping(value = "/user/detail", method = RequestMethod.GET)
    @ResponseBody
    public Object getUserInfo(@AuthenticationPrincipal User logedUser) {
        Optional<User> userOptional = Optional.ofNullable(logedUser);
        return userOptional.map(user -> BizResult.create(user, true, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg()))
                .orElse(BizResult.create(MsgCodeEnum.EXCEPTION.getErrCode(), MsgCodeEnum.EXCEPTION.getErrMsg()));
    }

    /**
     * 获取允许登录的角色列表
     *
     * @return
     */
    @RequestMapping(value = "/accessAllowedRole/list", method = RequestMethod.GET)
    @ResponseBody
    public Object getAccessAllowedRoleList() {
        List<RoleVO> roleVOList = new ArrayList<>();
        for (RoleCodeEnum roleCodeEnum : RoleCodeEnum.getAccessAllowedRoleList()) {
            RoleVO roleVO = new RoleVO();
            roleVO.setRoleCode(roleCodeEnum.getCode());
            roleVOList.add(roleVO);
        }
        return roleVOList;
    }

}
